package org.abratuhi.kvngroupware.pubsub;

public class Range<T> {
	
	Value<T> lowerBound;
	Value<T> upperBound;
	
	public Range(Value<T> l, Value<T> u){
		if(l == null || u == null) throw new IllegalArgumentException("Range's boundary values must be not NULL!");
		if(l.compareTo(u) > 0) throw new IllegalArgumentException(l.toString() + " > " + u.toString());
		setLowerBound(l);
		setUpperBound(u);
	}
	
	public Value<T> getLowerBound(){
		return this.lowerBound;
	}
	
	public Value<T> getUpperBound(){
		return this.upperBound;
	}
	
	private void setLowerBound(Value<T> l){
		this.lowerBound = l;
	}
	
	private void setUpperBound(Value<T> u){
		this.upperBound = u;
	}
	
	public boolean contains(Value<T> value){
		return this.lowerBound.compareTo(value) <= 0 && value.compareTo(this.upperBound) <= 0; 
	}
	
	public boolean intersects(Range<T> arange){
		//return (this.getLowerBound().compareTo(arange.getLowerBound()) <= 0 && this.getUpperBound().compareTo(arange.getLowerBound()) >= 0) ||
		//		(this.getLowerBound().compareTo(arange.getUpperBound()) <= 0 && this.getUpperBound().compareTo(arange.getUpperBound()) >= 0);
		return ( ! (this.getLowerBound().compareTo(arange.getLowerBound()) <= 0 && this.getUpperBound().compareTo(arange.getLowerBound()) <= 0) &&
				 ! (this.getLowerBound().compareTo(arange.getUpperBound()) >= 0 && this.getUpperBound().compareTo(arange.getUpperBound()) >= 0) );
	}

}
